/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-05 16:29:00
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import zyy.model.BeanRider;
import zyy.util.BaseException;
import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmAdminRider extends JDialog implements ActionListener {
  private static final long serialVersionUID = 27325420075172195L;
  private JPanel list = new JPanel();
  private Object tblRiderTitles[] = BeanRider.tblTitles;
  private Object tblRiderData[][];
  DefaultTableModel tabRiderModel = new DefaultTableModel();
  private JTable dataTableRider = new JTable(tabRiderModel);
  List<BeanRider> allRider = null;

  private JButton add = new JButton("添加骑手");
  private JButton del = new JButton("删除骑手");
  private JButton exit = new JButton("退出");
  private JPanel btns = new JPanel();

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.add) {
      System.out.println("管理员 添加骑手");
      FrmUserReg dlg1 = new FrmUserReg(null, "骑手注册", true, "骑手");
      dlg1.setVisible(true);
      this.reloadRiderTable();
    } else if (e.getSource() == this.del) {
      System.out.println("管理员 删除骑手");
      try {
        Bus.riderManager.del(Bus.curAdminRider.getRiderID());
        this.reloadRiderTable();
      } catch (BusinessException ex) {
        JOptionPane.showMessageDialog(null,
            String.format("骑手 %s:%s 删除失败\n", Bus.curAdminRider.getlName(), Bus.curAdminRider.getrName())
                + ex.getMessage(),
            "删除失败", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }
    } else if (e.getSource() == this.exit) {
      System.out.println("退出骑手管理");
      this.setVisible(false);
    }
  }

  public void reloadRiderTable() {
    try {
      allRider = Bus.riderManager.loadAll();
    } catch (BaseException e) {
      JOptionPane.showMessageDialog(null, e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
      return;
    }
    tblRiderData = new Object[allRider.size()][tblRiderTitles.length];
    for (int i = 0; i < allRider.size(); i++) {
      for (int j = 0; j < tblRiderTitles.length; j++)
        tblRiderData[i][j] = allRider.get(i).getCell(j);
    }
    tabRiderModel.setDataVector(tblRiderData, tblRiderTitles);
    // 限定列宽必须在reload里写死
    this.dataTableRider.validate();
    this.dataTableRider.repaint();
    System.out.println("成功重载 view Admin Rider 信息");
  }

  public FrmAdminRider() {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 管理员 骑手管理");
      this.setSize(1318, 838);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.list, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
      list.setLayout(new BorderLayout());
      list.add(new JScrollPane(this.dataTableRider), BorderLayout.CENTER);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.add);
      this.btns.add(this.del);
      this.btns.add(this.exit);
      this.add.addActionListener(this);
      this.del.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.add);
      Bus.jButtonZYYStyle(this.del);
      Bus.jButtonZYYStyle(this.exit);
    }
    // JTable 监听设置
    {
      this.dataTableRider.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
          int i = FrmAdminRider.this.dataTableRider.getSelectedRow();
          if (i < 0) {
            return;
          }
          Bus.curAdminRider = FrmAdminRider.this.allRider.get(i);
          FrmAdminRider.this.reloadRiderTable();
          FrmAdminRider.this.dataTableRider.setRowSelectionInterval(i, i);
          System.out.println("选中用户：" + Bus.curAdminRider);
        }
      });
    }
    // 内容初始化
    {
      this.reloadRiderTable();
      System.out.println("AdminRider every Work Cnt 共有列数：" + dataTableRider.getColumnModel().getColumnCount());
    }
  }
}